/*
 =============================================================================
 FILE...............: Main.java
 COMMENTS...........: Simple test code.
 =============================================================================
 AUTHOR(S)..........: Everton Ranielly de Sousa Cavalcante
 					  Department of Informatics and Applied Mathematics
 					  Federal University of Rio Grande do Norte
 LAST MODIFICATION..: November 26th, 2012
 ============================================================================= 					  
*/


/* ========================================================================= */
/* PACKAGES			                                                         */
/* ------------------------------------------------------------------------- */
package test;


/* ========================================================================= */
/* IMPORTS			                                                         */
/* ------------------------------------------------------------------------- */
import model.Booking;
import model.Client;
import model.Flight;
import model.Receipt;

import services.BookingManagementService;
import services.ConcreteDatabaseService;
import services.ConcreteMessagingService;
import services.ConcreteMessagingService;
import services.ConcretePaymentService;
import services.ConcreteStorageService;
import services.DatabaseService;
import services.MessagingService;
import services.PaymentService;
import services.StorageService;

import test.gui.FlightSearch;

/**
 * Simple test code
 * @author Everton Cavalcante
 */
public class Main 
{
	// Static instantiation of service providers
	private static BookingManagementService BookingManagementService = new BookingManagementService();
	private static DatabaseService DatabaseService = new ConcreteDatabaseService();
	private static PaymentService PaymentService = new ConcretePaymentService();
	private static StorageService StorageService = new ConcreteStorageService();
	private static MessagingService MessagingService = new ConcreteMessagingService();
	
	// Client
	private static Client client = new Client("Mr.", "Everton Cavalcante", "+558488089956", 
		"evertonranielly@gmail.com", "7217860313253768", "TP498176362");
	
	// Flight search window
	private static FlightSearch flightSearch = new FlightSearch();
	
	// Timer variables
	private static long begin, end;
	
	
	/**
	 * Main method
	 * @param args Command-line arguments
	 */
	public static void main(String[] args) 
	{		
		// Creates a new (parallel) thread
		Thread thread = new Thread()
		{
			public void run()
			{
				synchronized (flightSearch.lock)
				{
					while (flightSearch.isVisible())
					{
						try
						{ flightSearch.lock.wait(); }
						catch (InterruptedException e) 
		               	{ e.printStackTrace(); }
					}
							
					String fromCode = flightSearch.from.substring(0, flightSearch.from.indexOf(" "));
					String toCode = flightSearch.to.substring(0, flightSearch.to.indexOf(" "));
							
					// Begin timer
					begin = System.currentTimeMillis();
							
					/* Searches for available flights considering the user criteria
					 * Using: DatabaseService.searchFlight() */
					Flight[] availableFlights = DatabaseService.searchFlights(fromCode, toCode, 
						flightSearch.departure, flightSearch.arrival);
							
					if (availableFlights.length == 0 || availableFlights == null)
					{
						System.out.println("Error: There are no available flights for your request.");
						System.out.println("Please modify your search criteria and try again");
						System.exit(1);
					}
					else
					{
						/* Using: BookingManagementService.selectFlight() */
						Flight[] selectedFlights = BookingManagementService.selectFlights(availableFlights);
								
						/* Using: BookingManagementService.generateBooking() */
						Booking booking = BookingManagementService.generateBooking(selectedFlights, client);
							
						/* Using: PaymentService.makePayment() */
						boolean payment = PaymentService.makePayment(booking);
						if (payment)
						{
							/* Using: StorageService.generateReceipt() */
							Receipt receipt = StorageService.generateReceipt(booking);
									
							/* Using: DatabaseService.updateInformation() */
							DatabaseService.updateInformation(booking, receipt);
									
							/* Using: MessagingService.sendMessage() */
							String confirmation = MessagingService.sendMessage(receipt, client);
							System.out.println(confirmation);
						}
						else
						{
							System.out.println("Error:\nThe payment of your booking could not be authorized.");
							System.out.println("Please select another credit card or payment method.");
							System.exit(1);
						}
					}
				}
			}
		};
				
		thread.start();
				
		try 
		{ thread.join(); }
		catch (InterruptedException e) 
		{ e.printStackTrace(); }
				
		// End timer
		end = System.currentTimeMillis();
						
		// Execution time (in seconds)
		System.out.println("\nExecution time: " + ((end-begin) / 1000.0) + " seconds.");
		System.exit(0);
	}
}
